package com.inspire.myapp;

import java.util.Hashtable;

import com.codename1.components.SpanButton;
import com.codename1.io.ConnectionRequest;
import com.codename1.io.NetworkManager;
import com.codename1.io.Preferences;
import com.codename1.ui.Button;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.Graphics;
import com.codename1.ui.Image;
import com.codename1.ui.Label;
import com.codename1.ui.Slider;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.layouts.LayeredLayout;
import com.codename1.ui.plaf.Border;
import com.codename1.ui.plaf.Style;
import com.codename1.ui.table.TableLayout;

public class peripherique_item extends Container {
	public peripherique periph=null;
	public SpanButton b=null;
	public Label icon=null;
	public Slider s=null;
	public int width=0;
	public int height=0;
	public boolean initpaint =false;
	
	public peripherique_item(final peripherique periph) {
		super();
		Container butt=new Container(new LayeredLayout());
		this.setLayout(new BorderLayout());
		this.addComponent(BorderLayout.NORTH,butt);
		
		
		final Container cc1=new Container();
		cc1.setLayout(new BorderLayout());
		butt.addComponent(cc1);
		
		final Container cc=new Container();
		cc.setLayout(new BorderLayout());
		this.periph=periph;
		Label nomlabel=new Label(this.periph.nom);
		nomlabel.getStyle().setAlignment(LEFT, true);
		nomlabel.getStyle().setPadding(5, 5, 10, 5);
		cc.addComponent(BorderLayout.NORTH,nomlabel);
		final boolean favoris = Preferences.get("peripherique"+this.periph.id, false);
		Image ff=null;
		if (favoris) {
			ff=(Image) objets.obj.images.get("/images/favoris_icone_enable.png");
		}else{
			ff=(Image) objets.obj.images.get("/images/favoris_icone_disable.png");
		}
		ff=ff.scaledHeight(20);
		final Button fav = new Button(ff);
		fav.setUIID("Button");
		cc.addComponent(BorderLayout.WEST,fav);
		TableLayout ttt=new TableLayout(1,3);
		
		Container expr_cont=new Container(new TableLayout(3,1));	
		//Label l=new Label();
		//l.getStyle().setPadding(0, 15, 5, 5);
		//l.getStyle().setFgColor(25000, true);
		
		if (!periph.expr1.equals("")) {
			Label l=new Label();
			l.getStyle().setPadding(0, 15, 5, 5);
			l.getStyle().setFgColor(25000, true);
			//if (!l.getText().equals("")) l.setText(l.getText() + "\r\n");
			l.setText(l.getText() + periph.expr1);
			if (!l.getText().equals("")) expr_cont.addComponent(l);
		}
		if (!periph.expr2.equals("")) {
			Label l=new Label();
			l.getStyle().setPadding(0, 15, 5, 5);
			l.getStyle().setFgColor(25000, true);
			//if (!l.getText().equals("")) l.setText(l.getText() + "\r\n");
			l.setText(l.getText() + periph.expr2);
			if (!l.getText().equals("")) expr_cont.addComponent(l);
		}
		if (!periph.expr3.equals("")) {
			Label l=new Label();
			l.getStyle().setPadding(0, 15, 5, 5);
			l.getStyle().setFgColor(25000, true);
			//if (!l.getText().equals("")) l.setText(l.getText() + "\r\n");
			l.setText(l.getText() + periph.expr3);
			if (!l.getText().equals("")) expr_cont.addComponent(l);
		}
		//if (!l.getText().equals("")) expr_cont.addComponent(l);
		cc.addComponent(BorderLayout.CENTER,expr_cont);
		
		cc.setRTL(true);
		//icon=new Label("   ");
		//cc1.addComponent(BorderLayout.WEST,icon);
		cc1.getStyle().setPadding(0, 0, 90, 0);
		cc1.addComponent(BorderLayout.NORTH,cc);

		if (periph.ecriture_type.equals("VARIABLE") || periph.ecriture_type.equals("BINVAR")) {	
			s= new Slider();
			s.setMaxValue(100);
			s.setMinValue(0);
			s.setEditable(true);
			System.out.println(periph.nom + " ='"+periph.etat+"'");
			s.setProgress(Integer.valueOf(periph.etat));
			Container sc=new Container(new BorderLayout());
			sc.getStyle().setPadding(0, 0, 80, 60);
			sc.addComponent(BorderLayout.CENTER,s);
			this.addComponent(BorderLayout.CENTER,sc);
			s.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent evt) {
					Slider ss=(Slider) evt.getSource();
					int val= ss.getProgress();
					if (val==0) {
						periph.etat="0";
						Image mm=(Image) objets.obj.images.get("/"+periph.categorie.iconMin);
						//mm=mm.modifyAlpha((byte)255, 256*256*256-1);
						SpanButton bb=b;
						mm=mm.scaledHeight(50);
						bb.setIcon(mm);	
					} else {
						periph.etat=val+"";
						Image mm=(Image) objets.obj.images.get("/"+periph.categorie.iconMax);
						//mm=mm.modifyAlpha((byte)255, 256*256*256-1);
						SpanButton bb=b;
						mm=mm.scaledHeight(50);
						bb.setIcon(mm);	
					}
					cc.repaint();
					ConnectionRequest request = new ConnectionRequest();
					request.setUrl(Preferences.get("URL_NAS", objets.httpdefault)+"/index.php?action=setetat&valeur="+periph.etat+"&cmd=DIM&uuid="+periph.uuid);
					request.setPost(false);
					NetworkManager.getInstance().addToQueueAndWait(request);
					
				}
			});
		}
		
		//s=new Slider();
		//s.setMaxValue(30);
		//s.setMinValue(10);
		///s.setEditable(true);
		//s.setRenderPercentageOnTop(true);
		//s.setRTL(true);
		//this.addComponent(BorderLayout.SOUTH,s);	
		Container cbut=new Container(new BorderLayout());
		b = new SpanButton(" "/*this.periph.nom*/);
		
		cbut.addComponent(BorderLayout.CENTER,b);
		butt.addComponent(cbut);
		b.setUIID("Button");        	
    	b.setRTL(true);
    	//b.getStyle().setBgColor(8630,true);
    	b.setIconPosition("East");
		//b.setFocusable(true);
        //setLeadComponent(b);
    	if (this.periph.etat!=null && this.periph.etat.equals("1")) {
			Image mm=(Image) objets.obj.images.get("/"+this.periph.categorie.iconMax);
			//mm=mm.modifyAlpha((byte)255, 256*256*256-1);
			//mm.applyMask(mm.createMask());
			mm=mm.scaledHeight(50);
			b.setIcon(mm);	
		} else {
			Image mm=(Image) objets.obj.images.get("/"+this.periph.categorie.iconMin);
			//mm=mm.modifyAlpha((byte)255, 256*256*256-1);
			//mm.applyMask(mm.createMask());
			mm=mm.scaledHeight(50);
			b.setIcon(mm);	
		}
    	if (periph.ecriture_type.equals("BINAIRE") || periph.ecriture_type.equals("BINVAR")) {
    		b.setEnabled(true);
	    	b.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent evt) {
					System.out.println("Long pressed = " + evt.isLongEvent());
					if (evt.isLongEvent()==true) {
						
						Button bb=(Button) evt.getSource();
						boolean favoris = Preferences.get("peripherique"+periph.id, false);
						if (favoris) {
							favoris=false;
							Preferences.set("peripherique"+periph.id, favoris);
						} else {
							favoris=true;
							Preferences.set("peripherique"+periph.id, favoris);
							
						}
						Image ff=null;
						if (favoris) {
							ff=(Image) objets.obj.images.get("/images/favoris_icone_enable.png");
						}else{
							ff=(Image) objets.obj.images.get("/images/favoris_icone_disable.png");
						}
						ff=ff.scaledHeight(20);
						bb.setIcon(ff);	
						bb.setPressedIcon(null);
						bb.repaint();					
					} else {
						if (periph.etat.equals("1")) {
							periph.etat="0";
							Image mm=(Image) objets.obj.images.get("/"+periph.categorie.iconMin);
							//mm=mm.modifyAlpha((byte)255, 256*256*256-1);
							Button bb=(Button) evt.getSource();
							mm=mm.scaledHeight(50);
							bb.setIcon(mm);	
						} else {
							periph.etat="1";
							Image mm=(Image) objets.obj.images.get("/"+periph.categorie.iconMax);
							//mm=mm.modifyAlpha((byte)255, 256*256*256-1);
							Button bb=(Button) evt.getSource();
							mm=mm.scaledHeight(50);
							bb.setIcon(mm);	
						}
						cc.repaint();
						ConnectionRequest request = new ConnectionRequest();
						request.setUrl(Preferences.get("URL_NAS", objets.httpdefault)+"/index.php?action=setetat&valeur="+periph.etat+"&uuid="+periph.uuid);
						request.setPost(false);
						NetworkManager.getInstance().addToQueueAndWait(request);
					}
				}
			});    		
    	} else {
    		b.setEnabled(false);
    	}

		
	}
	
	@Override
	public void paint(Graphics g) {

		super.paint(g);
		if (!initpaint) init();
        //g.translate(getX(), getY());
        /*int w = this.getWidth();
        int h = this.getHeight();
        if (this.width!=w || this.height!=h){
        	init();
        	//this.paint(g);
        	this.width=w;
        	this.height=h;
        	//System.out.println("paint size " + w +"x"+h);
        }*/
        //g.setColor(256*256*256-1);
        //g.drawLine(0, h/2, w, h/2);
        //g.drawLine(0, h/2+1, w, h/2+1);
        
		//g.translate(-getX(), -getY());

	}

	public void init(){




        int w = this.getWidth();
        int h = this.getHeight();
        Image imgback = (Image) objets.obj.images.get("/images/block"+w+"x"+h);
        if (imgback==null) {
	    	//b.setText(this.periph.nom + "  H="+b.getPreferredH() + ", W="+Display.getInstance().getDisplayWidth());
			imgback=(Image) objets.obj.images.get("/images/block.png");
			imgback =utils.reformeImage(imgback, 15, 15, 15, 15, w, h);
			imgback=imgback.modifyAlpha((byte)255, 256*256*256-1);
			objets.obj.images.put("/images/block"+w+"x"+h, imgback);
        }
        
		utils.setBackground(getStyle(),imgback);
		utils.setBackground(getSelectedStyle(),imgback);
		utils.setBackground(getUnselectedStyle(),imgback);
		utils.setBackground(getPressedStyle(),imgback);  
		this.initpaint=true;
		this.repaint();
    	
	}
}
